package ntut.andengine.game;

import ntut.andengine.physics.ContactEvent;
import ntut.andengine.physics.PhysicalComponent;
import ntut.andengine.physics.PhysicalComponent.ComponentColor;
import ntut.andengine.physics.PhysicalFactory;

import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTexture;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import android.widget.Toast;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;

public class GameLevel5 extends GameLevel {

	PhysicalComponent bar1;
    PhysicalComponent bar2;
    GameLevel mLevel;
	
	@Override
	public void onLoadResources() {
		super.onLoadResources();
		mLevel = this;
		
		BitmapTexture backgroundBitmapTexture = new BitmapTexture(512, 1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		TextureRegion backgroundTextureRegion = BitmapTextureRegionFactory.createFromAsset(backgroundBitmapTexture, this, "universe.png", 0, 0);
		mEngine.getTextureManager().loadTexture(backgroundBitmapTexture);
		
		Sprite background = new Sprite(0, 0, backgroundTextureRegion);
		mScene.attachChild(background);
	}
	
	@Override
	public Scene onLoadScene() {
		
		PhysicalFactory.CreateWall(this, new Vector2(83, 713), 20, 100, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(182, 460), 180, 60, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(341, 109), 80, 80, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(449, 35), 60, 60, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(457, 257), 60, 160, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(327, 13), 160, 20, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(266, 85), 40, 120, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(415, 109), 60, 80, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(309, 425), 60, 240, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(412, 454), 120, 40, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(334, 748), 100, 40, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(264, 47), 240, 20, ComponentColor.Red);//40
        //PhysicalFactory.CreateWall(this, new Vector2(402, 164), 160, 20, ComponentColor.Red);//
        PhysicalFactory.CreateWall(this, new Vector2(391, 310), 60, 60, ComponentColor.Red);
        PhysicalFactory.CreateWall(this, new Vector2(383, 237), 80, 80, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(261, 256), 20, 200, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(121, 117), 40, 220, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(152, 308), 120, 40, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(112, 380), 40, 100, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(165, 624), 180, 60, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(398, 501), 160, 40, ComponentColor.Red);//
        PhysicalFactory.CreateWall(this, new Vector2(309, 690), 60, 60, ComponentColor.Red);
        PhysicalFactory.CreateWall(this, new Vector2(187, 689), 180, 40, ComponentColor.Red);//
        PhysicalFactory.CreateWall(this, new Vector2(172, 519), 200, 40, ComponentColor.Red);//
        PhysicalFactory.CreateWall(this, new Vector2(37, 714), 60, 160, ComponentColor.Red);
        PhysicalFactory.CreateWall(this, new Vector2(382, 554), 80, 60, ComponentColor.Red);
        PhysicalFactory.CreateWall(this, new Vector2(352, 625), 20, 80, ComponentColor.Red);
        PhysicalFactory.CreateWall(this, new Vector2(382, 696), 80, 60, ComponentColor.Red);
        PhysicalFactory.CreateWall(this, new Vector2(172, 566), 200, 40, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(382, 554), 80, 60, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(352, 625), 20, 80, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(382, 696), 80, 60, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(122, 269), 240, 40, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(45, 375), 80, 160, ComponentColor.Red);
        PhysicalFactory.CreateWall(this, new Vector2(188, 372), 100, 80, ComponentColor.Red);
        PhysicalFactory.CreateWall(this, new Vector2(228, 323), 20, 60, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(305, 235), 40, 80, ComponentColor.Red);
        PhysicalFactory.CreateWall(this, new Vector2(412, 386), 120, 80, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(426, 597), 100, 20, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(426, 653), 100, 20, ComponentColor.Red);//
        PhysicalFactory.CreateWall(this, new Vector2(221, 144), 160, 20, ComponentColor.Red);//
        
        PhysicalComponent gate = PhysicalFactory.CreateTransmissionGate(this, new Vector2(51, 44));
        gate.getBody().setUserData(new ContactEvent("Gate"){

			@Override
			public void beginContact(Body body, Vector2 Normal) {
				
				// LEVEL COMPLETE!!
				if(body.getUserData().toString() == "Player"){
					mLevel.runOnUiThread(new Runnable(){

						@Override
						public void run() {
							mMusic.pause();
							successSound.play();
							Toast.makeText(mLevel, "Level Completed!", Toast.LENGTH_SHORT).show();	
						}
			        	
			        });
					startActivity(GameLevel5.this, GameLevel18.class);
				}
			
			}

			@Override
			public void endContact(Body body, Vector2 Normal) {
				// TODO Auto-generated method stub
				
			}
		
		});
        
        bar1 = PhysicalFactory.CreateBar(this, new Vector2(100, 98), ComponentColor.Red, new Vector2(0, -10), false);
        bar2 = PhysicalFactory.CreateBar(this, new Vector2(100, 237), ComponentColor.Blue, new Vector2(0, 10), false);
        
        mSparkle = PhysicalFactory.CreateSparkle(this, new Vector2(455, 779), ComponentColor.Red);
	
		this.mScene.registerUpdateHandler(this.mPhysicsWorld);	
		mScene.registerUpdateHandler(new IUpdateHandler(){
			
			@Override
			public void onUpdate(float pSecondsElapsed) {
				
				CheckPutBack() ;
			}

			@Override
			public void reset() {
				// TODO Auto-generated method stub
				
			}
			
		});
		
		return mScene;
	}
	
	private void CheckPutBack(){
		
        if (bar1.getPosition().y > 237 || bar1.getPosition().y < 98){
            if(bar1.getPosition().y > 237 ){
            	bar1.setPosition(new Vector2(bar1.getPosition().x, 237f));
            }
            if(bar1.getPosition().y < 98 ){
            	bar1.setPosition(new Vector2(bar1.getPosition().x, 98f));
            }
        	bar1.setVelocity(bar1.getVelocity().mul(-1));
        }

        if (bar2.getPosition().y < 98 || bar2.getPosition().y > 237)
        {
            if(bar2.getPosition().y > 237 ){
            	bar2.setPosition(new Vector2(bar2.getPosition().x, 237f));
            }
            if(bar2.getPosition().y < 98 ){
            	bar2.setPosition(new Vector2(bar2.getPosition().x, 98f));
            }
        	bar2.setVelocity(bar2.getVelocity().mul(-1));
        }       
    }
}
